﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:Dlyndx
    /// </summary>
    public partial class Dlyndx
    {
        public Dlyndx()
        {
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long Vchcode)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from Dlyndx");
            strSql.Append(" where Vchcode=@Vchcode");
            var parameters = new SqlParameter[] { new SqlParameter("@Vchcode", SqlDbType.BigInt)
            };
            parameters[0].Value = Vchcode;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(DMZS.Model.Dlyndx model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into Dlyndx(");
            strSql.Append("Date,Number,VchType,Comment,btypeid,etypeid,ifcheck,checke,checkeDate,InputNo,draft,DeptID,isScan,SaleMan,SalePurpose)");
            strSql.Append(" values (");
            strSql.Append("@Date,@Number,@VchType,@Comment,@btypeid,@etypeid,@ifcheck,@checke,@checkeDate,@InputNo,@draft,@DeptID,@isScan,@SaleMan,@SalePurpose)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@Date", SqlDbType.DateTime),
                    new SqlParameter("@Number", SqlDbType.VarChar, 40),
                    new SqlParameter("@VchType", SqlDbType.Int, 4),
                    new SqlParameter("@Comment", SqlDbType.VarChar, 256),
                    new SqlParameter("@btypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@etypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@ifcheck", SqlDbType.Int, 4),
                    new SqlParameter("@checke", SqlDbType.VarChar, 25),
                    new SqlParameter("@checkeDate", SqlDbType.DateTime),
                    new SqlParameter("@InputNo", SqlDbType.VarChar, 25),
                    new SqlParameter("@draft", SqlDbType.Int, 4),
                    new SqlParameter("@DeptID", SqlDbType.VarChar, 25),
                    new SqlParameter("@isScan", SqlDbType.Int, 4),
                    new SqlParameter("@SaleMan", SqlDbType.VarChar, 25),
                    new SqlParameter("@SalePurpose", SqlDbType.VarChar, 200) };
            parameters[0].Value = model.Date;
            parameters[1].Value = model.Number;
            parameters[2].Value = model.VchType;
            parameters[3].Value = model.Comment;
            parameters[4].Value = model.btypeid;
            parameters[5].Value = model.etypeid;
            parameters[6].Value = model.ifcheck;
            parameters[7].Value = model.checke;
            parameters[8].Value = model.checkeDate;
            parameters[9].Value = model.InputNo;
            parameters[10].Value = model.draft;
            parameters[11].Value = model.DeptID;
            parameters[12].Value = model.isScan;
            parameters[13].Value = model.SaleMan;
            parameters[14].Value = model.SalePurpose;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.Dlyndx model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update Dlyndx set ");
            strSql.Append("Date=@Date,");
            strSql.Append("Number=@Number,");
            strSql.Append("VchType=@VchType,");
            strSql.Append("Comment=@Comment,");
            strSql.Append("btypeid=@btypeid,");
            strSql.Append("etypeid=@etypeid,");
            strSql.Append("ifcheck=@ifcheck,");
            strSql.Append("checke=@checke,");
            strSql.Append("checkeDate=@checkeDate,");
            strSql.Append("InputNo=@InputNo,");
            strSql.Append("draft=@draft,");
            strSql.Append("DeptID=@DeptID,");
            strSql.Append("isScan=@isScan,");
            strSql.Append("SaleMan=@SaleMan,");
            strSql.Append("SalePurpose=@SalePurpose");
            strSql.Append(" where Vchcode=@Vchcode");
            var parameters = new SqlParameter[] { new SqlParameter("@Date", SqlDbType.DateTime),
                    new SqlParameter("@Number", SqlDbType.VarChar, 40),
                    new SqlParameter("@VchType", SqlDbType.Int, 4),
                    new SqlParameter("@Comment", SqlDbType.VarChar, 256),
                    new SqlParameter("@btypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@etypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@ifcheck", SqlDbType.Int, 4),
                    new SqlParameter("@checke", SqlDbType.VarChar, 25),
                    new SqlParameter("@checkeDate", SqlDbType.DateTime),
                    new SqlParameter("@InputNo", SqlDbType.VarChar, 25),
                    new SqlParameter("@draft", SqlDbType.Int, 4),
                    new SqlParameter("@DeptID", SqlDbType.VarChar, 25),
                    new SqlParameter("@isScan", SqlDbType.Int, 4),
                    new SqlParameter("@SaleMan", SqlDbType.VarChar, 25),
                    new SqlParameter("@SalePurpose", SqlDbType.VarChar, 200),
                    new SqlParameter("@Vchcode", SqlDbType.BigInt, 8) };
            parameters[0].Value = model.Date;
            parameters[1].Value = model.Number;
            parameters[2].Value = model.VchType;
            parameters[3].Value = model.Comment;
            parameters[4].Value = model.btypeid;
            parameters[5].Value = model.etypeid;
            parameters[6].Value = model.ifcheck;
            parameters[7].Value = model.checke;
            parameters[8].Value = model.checkeDate;
            parameters[9].Value = model.InputNo;
            parameters[10].Value = model.draft;
            parameters[11].Value = model.DeptID;
            parameters[12].Value = model.isScan;
            parameters[13].Value = model.SaleMan;
            parameters[14].Value = model.SalePurpose;
            parameters[15].Value = model.Vchcode;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long Vchcode)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from Dlyndx ");
            strSql.Append(" where Vchcode=@Vchcode");
            var parameters = new SqlParameter[] { new SqlParameter("@Vchcode", SqlDbType.BigInt)
            };
            parameters[0].Value = Vchcode;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Vchcodelist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from Dlyndx ");
            strSql.Append(" where Vchcode in (" + Vchcodelist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.Dlyndx GetModel(long Vchcode)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 Vchcode,Date,Number,VchType,Comment,btypeid,etypeid,ifcheck,checke,checkeDate,InputNo,draft,DeptID,isScan,SaleMan,SalePurpose from Dlyndx ");
            strSql.Append(" where Vchcode=@Vchcode");
            var parameters = new SqlParameter[] { new SqlParameter("@Vchcode", SqlDbType.BigInt)
            };
            parameters[0].Value = Vchcode;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.Dlyndx DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.Dlyndx();
            if (row != null)
            {
                if (row["Vchcode"] != null && row["Vchcode"].ToString() != string.Empty)
                {
                    model.Vchcode = long.Parse(row["Vchcode"].ToString());
                }
                if (row["Date"] != null && row["Date"].ToString() != string.Empty)
                {
                    model.Date = DateTime.Parse(row["Date"].ToString());
                }
                if (row["Number"] != null)
                {
                    model.Number = row["Number"].ToString();
                }
                if (row["VchType"] != null && row["VchType"].ToString() != string.Empty)
                {
                    model.VchType = int.Parse(row["VchType"].ToString());
                }
                if (row["Comment"] != null)
                {
                    model.Comment = row["Comment"].ToString();
                }
                if (row["btypeid"] != null)
                {
                    model.btypeid = row["btypeid"].ToString();
                }
                if (row["etypeid"] != null)
                {
                    model.etypeid = row["etypeid"].ToString();
                }
                if (row["ifcheck"] != null && row["ifcheck"].ToString() != string.Empty)
                {
                    model.ifcheck = int.Parse(row["ifcheck"].ToString());
                }
                if (row["checke"] != null)
                {
                    model.checke = row["checke"].ToString();
                }
                if (row["checkeDate"] != null && row["checkeDate"].ToString() != string.Empty)
                {
                    model.checkeDate = DateTime.Parse(row["checkeDate"].ToString());
                }
                if (row["InputNo"] != null)
                {
                    model.InputNo = row["InputNo"].ToString();
                }
                if (row["draft"] != null && row["draft"].ToString() != string.Empty)
                {
                    model.draft = int.Parse(row["draft"].ToString());
                }
                if (row["DeptID"] != null)
                {
                    model.DeptID = row["DeptID"].ToString();
                }
                if (row["isScan"] != null && row["isScan"].ToString() != string.Empty)
                {
                    model.isScan = int.Parse(row["isScan"].ToString());
                }
                if (row["SaleMan"] != null)
                {
                    model.SaleMan = row["SaleMan"].ToString();
                }
                if (row["SalePurpose"] != null)
                {
                    model.SalePurpose = row["SalePurpose"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select Vchcode,Date,Number,VchType,Comment,btypeid,etypeid,ifcheck,checke,checkeDate,InputNo,draft,DeptID,isScan,SaleMan,SalePurpose ");
            strSql.Append(" FROM Dlyndx ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Vchcode,Date,Number,VchType,Comment,btypeid,etypeid,ifcheck,checke,checkeDate,InputNo,draft,DeptID,isScan,SaleMan,SalePurpose ");
            strSql.Append(" FROM Dlyndx ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Dlyndx ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.Vchcode desc");
            }
            strSql.Append(")AS Row, T.*  from Dlyndx T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

       

        public virtual DataTable GetBarcode(long ID, int OrderType)
        {
            var strSql = @"
select b.ID,b.ID BarcodeID,b.Code,b.ProductID,b.CreateTime,b.Status,b.PrintCount,b.ReturnCount,b.ParentID,
	bs.OrderTypeID,bs.OrderID,bs.OrderLineID,
	p.Code ProductCode,p.Name ProductName,
    bs.ID BarcodeStorageID
from Barcode b
join BarcodeStorageOperation bs on bs.BarcodeID=b.ID
join Product p on p.ID=b.ProductID
where bs.OrderTypeID=@OrderTypeID and bs.OrderID=@OrderID
";
            var parameters = new SqlParameter[] { 
                new SqlParameter("@OrderTypeID", SqlDbType.Int,4),
                new SqlParameter("@OrderID", SqlDbType.BigInt, 8)};
            parameters[0].Value = OrderType;
            parameters[1].Value = ID;

            DataSet ds = DbHelperSQL.Query(strSql, parameters);
            if (ds == null) return null;

            return ds.Tables[0];
        }
    }
}

